package yxy.game.pm2.active.command.warlog;

import yxy.apple.logger.Logger;
import yxy.cherry.data.bean.actsecretcharge;
import yxy.cherry.data.bean.actsecretreward;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.active.module.ActiveModule_征战日志;
import yxy.game.pm2.active.module.DrawStatus;
import yxy.game.pm2.active.module.actdata.ActDataCenter;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.bean.mq.CCReason;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.ActiveMessage;
import yxy.game.pm2.message.ActiveMessage.ActWarLogDrawRewardRequest;
import yxy.game.pm2.message.PackageMessage;
import yxy.game.pm2.module.CurrencyHandle;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Map;

@MessageType(proto = Command.ActWarLogDrawReward, classtype = ActiveMessage.ActWarLogDrawRewardRequest.class)
public class CMD_Active_征战日志_DrawReward extends Command<ActWarLogDrawRewardRequest> {

	private static final Logger logger = Logger.create(CMD_Active_征战日志_DrawTaskReward.class);
	
	@Override
	public void execute(MessageSource messageSource, ActWarLogDrawRewardRequest request) throws Exception {

		String playercode = messageSource.getTag();
		ActiveMessage.ActWarLogDrawRewardResponse.Builder response = ActiveMessage.ActWarLogDrawRewardResponse.newBuilder();
		
		int type = request.getType();
		int id = request.getId();
		
		ActiveModule_征战日志 am = ActiveModule_征战日志.get(playercode,false);
		if(am == null) {
			send(messageSource, response, Result.ActNoOpen);
			return;
		}
		
		if(am.getRunday() < 7) {
			send(messageSource, response, Result.ActNoOpen);
			return;
		}
		
		if(type != 1 && type != 2) {
			send(messageSource, response, Result.DataError);
			return;
		}
		
		// 1领取经验奖励 
		if(type == 1) {
			if(am.getZzrewards().contains(id)) {
				send(messageSource, response, Result.ActHadGet);
				return;
			}
			
			actsecretreward asr = ActDataCenter.getData(id, actsecretreward.class, playercode);
			if(asr == null) {
				logger.info("id 不存在 code：" + playercode + " id：" + id);
				send(messageSource, response, Result.DataError);
				return;
			}
			
			int zzexp = am.getZzexp();
			if(zzexp < asr.getCondition()) {
				send(messageSource, response, Result.ActCannotGet);
				return;
			}
			
			ArrayList<Currency> curs = (ArrayList<Currency>) Currency.create(asr.getRewardtype(), asr.getRewardid(), asr.getRewardnum());
			Collection<PackageMessage.Item> items = new CurrencyHandle().packItems(curs);
			response.addAllDisplay(items);
			response.setType(type);
			
			am.getZzrewards().add(id);
			am.set();
			
			new CurrencyHandle().addCurrency(playercode, CCReason.活动_征战日志, curs);
			
			//经验奖励
			Map<String, actsecretreward> asrmap = ActDataCenter.getAllData(actsecretreward.class, playercode);
			for(String key : asrmap.keySet()) {
				actsecretreward data = asrmap.get(key);
				ActiveMessage.WarLogRewards.Builder expreward = ActiveMessage.WarLogRewards.newBuilder();
				expreward.setId(data.getId());
				expreward.setCondition(data.getCondition());
				
				ArrayList<Currency> curs2 = (ArrayList<Currency>) Currency.create(data.getRewardtype(), data.getRewardid(), data.getRewardnum());
				Collection<PackageMessage.Item> items2 = new CurrencyHandle().packItems(curs2);
				expreward.addAllCur(items2);
				
//				boolean a = am.getZzrewards().contains(data.getId());
				int canget = am.getZzrewards().contains(data.getId()) ? DrawStatus.hadget.getStatus() : am.getZzexp() >= data.getCondition() ? DrawStatus.canget.getStatus() : DrawStatus.cannotget.getStatus(); 
				expreward.setCanget(canget);
				
				response.addRewards(expreward.build());
			}
			
			send(messageSource, response, Result.Success);
			return;
		}
	
		//2领取充值奖励
		if(type == 2) {
			if(am.getPayrewards().contains(id)) {
				send(messageSource, response, Result.ActHadGet);
				return;
			}
			
			actsecretcharge asc = ActDataCenter.getData(id, actsecretcharge.class, playercode);
			if(asc == null) {
				logger.info("id 不存在 code：" + playercode + " id：" + id);
				send(messageSource, response, Result.DataError);
				return;
			}
			
			int payamount = am.getPayamount();
			if(payamount < asc.getCondition()) {
				send(messageSource, response, Result.ActCannotGet);
				return;
			}
			
			ArrayList<Currency> curs = (ArrayList<Currency>) Currency.create(asc.getRewardtype(), asc.getRewardid(), asc.getRewardnum());
			Collection<PackageMessage.Item> items = new CurrencyHandle().packItems(curs);
			response.addAllDisplay(items);
			response.setType(type);
			
			am.getPayrewards().add(id);
			am.set();
			
			new CurrencyHandle().addCurrency(playercode, CCReason.活动_征战日志, curs);
			
			//充值奖励
			Map<String, actsecretcharge> ascmap = ActDataCenter.getAllData(actsecretcharge.class, playercode);
			for(String key : ascmap.keySet()) {
				actsecretcharge execl = ascmap.get(key);
				ActiveMessage.WarLogRewards.Builder payreward = ActiveMessage.WarLogRewards.newBuilder();
				payreward.setId(execl.getId());
				payreward.setCondition(execl.getCondition());
				
				ArrayList<Currency> execlcurs = (ArrayList<Currency>) Currency.create(execl.getRewardtype(), execl.getRewardid(), execl.getRewardnum());
				Collection<PackageMessage.Item> execlitems = new CurrencyHandle().packItems(execlcurs);
				payreward.addAllCur(execlitems);
				
				int canget = am.getPayrewards().contains(execl.getId()) ? DrawStatus.hadget.getStatus() : 
						am.getPayamount() >= execl.getCondition() ? DrawStatus.canget.getStatus() : DrawStatus.cannotget.getStatus(); 
				payreward.setCanget(canget);
				
				response.addRewards(payreward.build());
			}
			send(messageSource, response, Result.Success);
			return;
		}
		
	}

	private void send(MessageSource messageSource, ActiveMessage.ActWarLogDrawRewardResponse.Builder response, Result result) {
		response.setResult(result.packResult());
		MessageEmitterHandler.getInstance().send(messageSource, response.build());
	}
}
